<?php
namespace app\worker\service;

use app\common\helper\FormatHelper;
use think\Db;

class VoiceService {

    /**
     * 获取最新的语音播报
     */
    public static function getNewVoice($users_id,$type,$voice_type=0){

        $userInfo = Db::name('qt_users')->where('id',$users_id)->find();
        if (empty($userInfo)) {
            return FormatHelper::fReturn('没有找到用户信息！','',FormatHelper::ERROR);
        }
        $voice_config = Db::name('qt_voice_config')->where('store_id',$userInfo['store_id'])->find();
        if (empty($voice_config)) {
            return FormatHelper::fReturn('没有找到门店下的配置！','',FormatHelper::ERROR);
        }
        $query = Db::name('qt_send_voice_msg')->where('status',2)
            ->where('store_id',$userInfo['store_id'])
            ->where('is_create_voice',1);

        if (!empty($type)) {
            $query->where('type',$type);
        }
        if (!empty($voice_type)) {
            $query->where('voice_type',$voice_type);
        }

        $msg_data = $query->order('id asc')
            ->limit(10)
            ->select();

        if (!empty($msg_data)) {
            //查询后更新播放状态
            Db::name('qt_send_voice_msg')->whereIn('id', array_column($msg_data,'id'))->update(['status'=>1,'play_time'=>time()]);
        }

        return FormatHelper::fReturn('查询成功',$msg_data);
    }



    /**
     * 获取最新的语音播报
     */
    public static function getEngineerVoice($users_id){

        $userInfo = Db::name('qt_users')->where('id',$users_id)->find();
        if (empty($userInfo)) {
            return FormatHelper::fReturn('没有找到用户信息！','',FormatHelper::ERROR);
        }
        $voice_config = Db::name('qt_voice_config')->where('store_id',$userInfo['store_id'])->find();
        if (empty($voice_config)) {
            return FormatHelper::fReturn('没有找到门店下的配置！','',FormatHelper::ERROR);
        }
        $query = Db::name('qt_send_voice_msg')->where('status',2)
            ->where('store_id',$userInfo['store_id'])
            ->where(['engineer_id'=>$users_id])
            ->where('is_create_voice',1);
//        if (!empty($type)) {
//            $query->where('type',$type);
//        }
        $msg_data = $query->order('id asc')
            ->limit(10)
            ->select();

        if (!empty($msg_data)) {
            //查询后更新播放状态
            Db::name('qt_send_voice_msg')->whereIn('id', array_column($msg_data,'id'))->update(['status'=>1,'play_time'=>time()]);
        }

        return FormatHelper::fReturn('查询成功',$msg_data);
    }




}
